package com.wsh.controller.sys;

import com.wsh.dto.sys.RoleDto;
import com.wsh.utils.JsonResult;
import com.wsh.base.BaseVo;
import com.wsh.entity.sys.RoleEntity;
import com.wsh.vo.sys.RoleVo;
import com.wsh.sys.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

/**
 * 权限数据接口
 */
@RestController
@RequestMapping("/sys/role")
@Api(tags = "系统：角色管理")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @PreAuthorize("@el.check('role:list')")
    @GetMapping("/list")
    @ApiOperation(value = "角色列表", notes = "查询数据库中的记录", httpMethod = "GET",response = Object.class)
    public Object list(RoleDto roleDto) {
        return JsonResult.success(roleService.getList(roleDto));
    }

    /**
     * 查询角色
     */
    @GetMapping("/getById")
    @ApiOperation(value = "查询角色")
    public Object getById(@RequestParam String formId) {
        RoleEntity roleEntity = roleService.getById(formId);
        RoleVo roleVo = new RoleVo();
        BeanUtils.copyProperties(roleEntity, roleVo);
        return JsonResult.success(new BaseVo(roleVo));
    }

    /**
     * 用户下拉树
     */
    @GetMapping("/getRoleOption")
    @ApiOperation(value = "用户下拉树")
    public Object getRoleOption() {
        return JsonResult.success(roleService.list());
    }

    /**
     * 保存权限
     */
    @PreAuthorize("@el.check('role:save')")
    @PostMapping("/saveRole")
    @ApiOperation(value = "保存权限")
    public Object saveRole(@RequestBody RoleEntity roleEntity) {
        // 添加
        if (StringUtils.isEmpty(roleEntity.getRoleId())) {
            roleEntity.setCreateTime(new Date());
        }
        return JsonResult.success(roleService.saveOrUpdate(roleEntity));
    }

}
